package com.example.myapplication.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.bean.NewsBean;

import java.util.List;

public class NewsAdapter extends BaseAdapter {
    Context context;
    List<NewsBean.ResultBean.DataBean> resultBeanList;

    public NewsAdapter(Context context, List<NewsBean.ResultBean.DataBean> resultBeanList) {
        this.context = context;
        this.resultBeanList = resultBeanList;
    }

    @Override
    public int getCount() {
        return resultBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return resultBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       if (convertView == null){
           convertView = View.inflate(context, R.layout.news_item_layout,null);
       }
        ImageView newsPicIv = convertView.findViewById(R.id.news_pic_iv);
        TextView newsTitleTv = convertView.findViewById(R.id.news_title_tv);
        TextView newsTimesTv = convertView.findViewById(R.id.news_time_tv);
        newsTitleTv.setText(resultBeanList.get(position).getTitle()+"");
        newsTimesTv.setText(resultBeanList.get(position).getDate()+"");
        Glide.with(context).load(resultBeanList.get(position).getThumbnail_pic_s()).into(newsPicIv);
        return convertView;
    }
}
